package com.naire.modul.projecplan.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.common.dao.GeneralDaoImpl;
import net.common.service.PageBean;
import net.common.util.BeanUtils;

import com.naire.modul.projecperiods.pojo.Projecperiods;
import com.naire.modul.projecplan.dao.ProjecplanDao;
import com.naire.modul.projecplan.pojo.Projecplan;
import com.naire.modul.projecplan.vo.ProjecplanVo;

public class ProjecplanDaoImpl extends GeneralDaoImpl implements ProjecplanDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<ProjecplanVo> search(PageBean pageBean, Integer periods_id)
			throws Exception {
		StringBuilder hql = new StringBuilder(
				"from Projecplan where projecperiods.periods_id=:periods_id");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("periods_id", periods_id);
		List<ProjecplanVo> d = new ArrayList<ProjecplanVo>();
		List<Projecplan> l = hibernateHandler.query(hql, pageBean, params);
		for (Projecplan bean : l) {
			ProjecplanVo dv = new ProjecplanVo();
			BeanUtils.copyProperties(bean, dv);
			d.add(dv);
		}
		return d;
	}

	@Override
	public void insert(ProjecplanVo bean) throws Exception {
		Projecperiods p = (Projecperiods) hibernateHandler.find(
				Projecperiods.class, bean.getPeriods_id());
		String code = p.getProject().getProject_num();
		Projecplan pojo = new Projecplan();
		BeanUtils.copyProperties(bean, pojo);
		pojo.setProjecperiods(p);
		pojo.setProject_code(code);
		hibernateHandler.insert(pojo);
	}

	@Override
	public void update(ProjecplanVo bean) throws Exception {
		Projecplan pojo = (Projecplan) hibernateHandler.find(Projecplan.class,
				bean.getPlan_id());
		BeanUtils.copyProperties(bean, pojo);
		hibernateHandler.update(pojo);
	}

	@Override
	public void updateFlag(ProjecplanVo bean) throws Exception {
		update(bean);
	}

	@Override
	public ProjecplanVo find(Integer id) throws Exception {
		Projecplan pojo = (Projecplan) hibernateHandler.find(Projecplan.class,
				id);
		ProjecplanVo bean = new ProjecplanVo();
		BeanUtils.copyProperties(pojo, bean);
		return bean;
	}

	@Override
	public void delete(Integer id) throws Exception {
		Projecplan pojo = (Projecplan) hibernateHandler.find(Projecplan.class,
				id);
		hibernateHandler.delete(pojo);
	}
}
